<?php

/*
  Document   : Beheer database pagina
  Created on : 12-mei-2011, 12:05:08
  Author     : Shahram abdi
 */

// load configuration file
require_once('../config/config.php');
require_once('../config/error_handler.php');

// Chat class that contains server-side chat functionality
class Beheer {

    // database handler
    private $mMysqli;

    // constructor opens database connection
    function __construct() {
        // connect to the database
        $this->mMysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
    }

    // destructor closes database connection
    public function __destruct() {
        $this->mMysqli->close();
    }

    //The postMessages method inserts a profile into the database
    public function postMessage($titel, $tekst, $link) {

        // escape the variable data for safely adding them to the database
        $titel = $this->mMysqli->real_escape_string($titel);
        $tekst = $this->mMysqli->real_escape_string($tekst);
        $link = $this->mMysqli->real_escape_string($link);

        // build the SQL query that adds a new message to the server
        $query = 'INSERT INTO nieuws(datum, titel, tekst, link) ' .
                'VALUES (NOW(), "' . $titel . '","' . $tekst . '","' . $link . '")';
        // execute the SQL query
        $result = $this->mMysqli->query($query);
        echo 'Nieuws is toegevoegd.';
    }

    public function retrieveAllNews() {
        $query = "SELECT * FROM nieuws ORDER BY id_nieuws DESC ";
        // execute the query
        $result = $this->mMysqli->query($query);

        // build the JSON response
        $response = array();
        $response['messages'] = array();

        // check to see if we have any results
        if ($result->num_rows) {
            // loop through all the fetch
            while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
                $message = array();
                $message['id_nieuws'] = $row['id_nieuws'];
                $message['titel'] = $row['titel'];
                $message['tekst'] = $row['tekst'];
                $message['link'] = $row['link'];
                $message['datum'] = $row['datum'];
                array_push($response['messages'], $message);
            }
            // close the database connection as soon as possible
            $result->close();
        }
        // return the JSON response
        return $response;
    }

    public function deleteNieuws($id) {
        // escape the variable data for safely adding them to the database
        $titel = $this->mMysqli->real_escape_string($id);
        // build the SQL query that adds a new message to the server
        $query = 'DELETE FROM nieuws WHERE id_nieuws=' . $id;
        // execute the SQL query
        $result = $this->mMysqli->query($query);
        echo 'Nieuws is verwijderd.';
    }

    public function retrieveAllContracts() {
        $query = "SELECT werkgeverID, instellingnaam FROM werkgeverprofiel ORDER BY werkgeverID DESC ";
        // execute the query
        $result = $this->mMysqli->query($query);

        // build the JSON response
        $response = array();
        $response['messages'] = array();

        // check to see if we have any results
        if ($result->num_rows) {
            // loop through all the fetch
            while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
                $message = array();
                $message['werkgeverID'] = $row['werkgeverID'];
                $message['instellingnaam'] = $row['instellingnaam'];
                array_push($response['messages'], $message);
            }
            // close the database connection as soon as possible
            $result->close();
        }
        // return the JSON response
        return $response;
    }

    public function deleteContracts($id) {
        // escape the variable data for safely adding them to the database
        $titel = $this->mMysqli->real_escape_string($id);
        // build the SQL query that adds a new message to the server
        $query = 'DELETE FROM werkgeverprofiel WHERE werkgeverID=' . $id;
        // execute the SQL query
        $result = $this->mMysqli->query($query);
        echo 'Contract is verwijderd.';
    }

    public function retrieveAllStudents() {
        $query = "SELECT studentID, voornaam, achternaam FROM studentprofiel ORDER BY studentID DESC ";
        // execute the query
        $result = $this->mMysqli->query($query);

        // build the JSON response
        $response = array();
        $response['messages'] = array();

        // check to see if we have any results
        if ($result->num_rows) {
            // loop through all the fetch
            while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
                $message = array();
                $message['studentID'] = $row['studentID'];
                $message['voornaam'] = $row['voornaam'];
                $message['achternaam'] = $row['achternaam'];
                array_push($response['messages'], $message);
            }
            // close the database connection as soon as possible
            $result->close();
        }
        // return the JSON response
        return $response;
    }

    public function deleteStudents($id) {
        // escape the variable data for safely adding them to the database
        $titel = $this->mMysqli->real_escape_string($id);
        // build the SQL query that adds a new message to the server
        $query = 'DELETE FROM studentprofiel WHERE studentID=' . $id;
        // execute the SQL query
        $result = $this->mMysqli->query($query);
        echo 'Student is verwijderd.';
    }
}
?>
